﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quality.BasicData
{
    public partial class organization : Form
    {
        public organization()
        {
            InitializeComponent();
        }
        private void organization_Load(object sender, EventArgs e)
        { 
           sqlHelper.setTreeView(treeView1, 0);
           
          
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("请填写要添加的节点名称！");
                return;
            }
            if (treeView1.Nodes.Count <= 0)
            {
                string sql = "insert into organization(nodeName,parentId) output inserted.id values" + "(" + " " + "'" + textBox1.Text.Trim() + "'" + "," + "'" + 0 + "'" + ")";
                int id = (int)sqlHelper.ExecuteScalar(sql);
                TreeNode node1 = new TreeNode();
                node1.Tag = id;
                node1.Text = textBox1.Text.Trim();
                treeView1.Nodes.Add(node1);
                textBox1.Text = "";
               
            }
            else 
            {
                if (treeView1.SelectedNode == null)
                {
                    MessageBox.Show("请选择父节点");
                    return;
                }
            int id = (int)treeView1.SelectedNode.Tag;
            string sql = "insert into organization(nodeName,parentId) output inserted.id values" + "(" + " " + "'" + textBox1.Text.Trim() + "'" + "," + "'" + id + "'" + ")";
            int id1 = (int)sqlHelper.ExecuteScalar(sql);
            TreeNode node1 = new TreeNode();
            node1.Tag = id1;
            node1.Text = textBox1.Text.Trim();
            treeView1.SelectedNode.Nodes.Add(node1);
            textBox1.Text = "";

            }
            

           
        }

        private void button3_Click(object sender, EventArgs e)
        {

           
            if (treeView1.SelectedNode == null)
            {
                MessageBox.Show("请选择要删除的节点！");
                return;
            }
            if (treeView1.SelectedNode.Nodes.Count>0)
            {
                MessageBox.Show("请先删除子节点！");
                return;
            }
            int count = sqlHelper.Count("select * from users where org='" + (int)treeView1.SelectedNode.Tag + "'");
            if (count > 0) { MessageBox.Show("部门资料已在用户资料中被使用，不能删除！"); return; }
            //选中节点的主键id，也是其子节点的parentId
            int id = (int)treeView1.SelectedNode.Tag;
            nodeDelete(id);
            treeView1.SelectedNode.Remove();
        }

        //数据表中的数据的递归删除方法
        public void nodeDelete(int id)
        {
            string sql = "select * from organization where parentId=" + id;
            DataTable ds = sqlHelper.ExecuteDataTable(sql);
            if (ds.Rows.Count > 0)
            {
                //有子节点
                foreach (DataRow row in ds.Rows)
                {
                    //先删除父节点
                    string delete = "delete from organization where id=" + id;
                    int k = sqlHelper.ExecuteNonQuery(delete);
                    //查找子节点，删除
                    int id1 = (int)row["id"];
                    nodeDelete(id1);
                }
            }
            else
            {
                //没有子节点
                string delete = "delete from organization where id=" + id;
                int k = sqlHelper.ExecuteNonQuery(delete);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                MessageBox.Show("请选择节点！");
                return;
            }
            
           

            int id = (int)treeView1.SelectedNode.Tag;

            string delete = "update organization set nodeName='" + textBox2.Text.Trim() + "' where id=" + id;
            int k = sqlHelper.ExecuteNonQuery(delete);
          treeView1.SelectedNode.Text=textBox2.Text;
          
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            textBox2.Text=treeView1.SelectedNode.Text;
        }

        private void organization_Shown(object sender, EventArgs e)
        {
            treeView1.SelectedNode = null;
            textBox2.Text = null;
        }

       

    }
}
